/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.frm.ccm.entity;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import merlion.crm.slcm.entity.Customer;

/**
 *
 * @author Kenn and Yuwei
 */
@Entity
public class CreditAccount implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private Timestamp creditAccountDate;
    private String creditAccountDateStr;
    private boolean isDefault;//Default: true, Active:false
    private double creditUsed;
    private double creditBalance;
    private boolean currency;//true for SGD, false for USD

    @ManyToOne(targetEntity = CreditGroup.class, cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER)
    private CreditGroup creditGroup;
    
    @OneToOne(targetEntity = Customer.class, cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER)
    private Customer customer;
    
    private static final SimpleDateFormat formatter = new SimpleDateFormat("MMMMM dd, yyyy");
    
    //Constructor
    public CreditAccount(){
        creditAccountDate = new Timestamp(new Date().getTime());
        creditAccountDateStr = convertToDateFormat(creditAccountDate);
        isDefault = Boolean.FALSE;
        creditUsed = (double)0;
        creditBalance = (double)0;
        currency = Boolean.TRUE;
        creditGroup = new CreditGroup();
        customer = null;
    }
    private String convertToDateFormat(Timestamp timeStamp){
        return formatter.format((java.util.Date) timeStamp);
    }
    //Getter and Setter
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public CreditGroup getCreditGroup() {
        return creditGroup;
    }

    public void setCreditGroup(CreditGroup creditGroup) {
        this.creditGroup = creditGroup;
    }

    public boolean isIsDefault() {
        return isDefault;
    }

    public void setIsDefault(boolean isDefault) {
        this.isDefault = isDefault;
    }

    public Timestamp getCreditAccountDate() {
        return creditAccountDate;
    }

    public void setCreditAccountDate(Timestamp creditAccountDate) {
        this.creditAccountDate = creditAccountDate;
    }

    public String getCreditAccountDateStr() {
        return creditAccountDateStr;
    }

    public void setCreditAccountDateStr(String creditAccountDateStr) {
        this.creditAccountDateStr = creditAccountDateStr;
    }

    public double getCreditUsed() {
        return creditUsed;
    }

    public void setCreditUsed(double creditUsed) {
        this.creditUsed = creditUsed;
    }

    public boolean isCurrency() {
        return currency;
    }

    public void setCurrency(boolean currency) {
        this.currency = currency;
    }

    public double getCreditBalance() {
        return creditBalance;
    }

    public void setCreditBalance(double creditBalance) {
        this.creditBalance = creditBalance;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    
    //Object Method
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof CreditAccount)) {
            return false;
        }
        CreditAccount other = (CreditAccount) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.frm.ccm.entity.CreditAccount[ id=" + id + " ]";
    }
    
}
